package com.wcj.sort;

public class 冒泡排序 {

    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,4,5,6,7,8};
        bubbleSort(arr);
        for (int i : arr) {
            System.out.println(i);
        }
    }

    public static void bubbleSort(int[] arr){
        int count = 0;
        for (int i = arr.length - 1; i >= 0; i--){
            boolean flag = true;
            for(int j = 0; j < i; j++){
                if (arr[j]>arr[j+1]){
                    flag = false;
                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
                System.out.println("循环次数："+ ++count);
            }
            if (flag){
                break;
            }
        }
    }
}
